// C Source File
// Created 11/19/2007; 7:48:06 PM

#include "Civilization.h"
#include "Technology.h"
#include "Interface.h"
#include "Terrain.h"
#include "Crash.h"
#include "Game.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Highlight //////////////////////////////////////////////////////////////////////////////////////////////////////
// highlights the requested area of text //////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Highlight2(int x, int y, int width) 
{
	int loop;
	for (loop =  0; loop < 11; loop++)
		DrawLine(x, loop + y, x+width, loop + y, A_XOR);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SideMenu ///////////////////////////////////////////////////////////////////////////////////////////////////////
// creates a menu on the side of the screen ///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char SideMenu(char item[20][17], char size, int x, int y)
{
	char num = (93 - y) / 10 + 1;
	char firstIndex = 0;
	char index = 0;
	int key = 0;
	
	while(key != KEY_ESC && key != KEY_ENTER)
	{
		ClrSide2(y);
		char z = min(firstIndex+num+1, size), i;
		for(i = firstIndex;i < z; i++)
			DrawStr(x, y + (i-firstIndex)*10, item[i], A_NORMAL);
			
		char j = index - firstIndex;
		bool redraw = 0;
		
		while(!redraw && key != KEY_ESC && key != KEY_ENTER)
		{
			Highlight2(x, y + j*10 - 3, 49);
			key = ngetchx();
			Highlight2(x, y + j*10 - 3, 49);
			
			if(key == KEY_DOWN)
			{
				if(index < size - 1)
				{
					index++;
					if(index > firstIndex + num - 1)
					{
						redraw = 1;
						if(firstIndex + num + num/2 > size - 1)
							firstIndex = size - num;
						else
							firstIndex += num/2;
					} 
				}
			}
			else if(key == KEY_UP)
			{
				if(index > 0)
				{
					index--;
					if(index < firstIndex)
					{
						redraw = 1;
						if(firstIndex - num/2 < 0)
							firstIndex = 0;
						else
							firstIndex -= num/2;
					}
				}
			}
			
			j = index - firstIndex;
		}
	}
	
	if(key == KEY_ESC) return -1;
	return index;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Menu ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// creates a menu on the screen ///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char Menu(char unitI[11], char unitISize, char buildingI[50], char city, char size)
{
	char num = 10;
	char firstIndex = 0;
	char index = 0;
	int key = 0;
	
	while(key != KEY_ESC && key != KEY_ENTER)
	{
		Clr(70, 0, 160, 98);
		char z = min(firstIndex+num+1, size), i;
		for(i = firstIndex; i < z; i++)
		{
			if(i < unitISize)
			{
				DrawStr(70, 2 + (i-firstIndex)*10, _units[unitI[i]].name, A_NORMAL);
				Sprite16(140, 2 + (i-firstIndex)*10 - 2, SPRITE_HEIGHT, &game.unitpic[_units[unitI[i]].pic*SPRITE_HEIGHT], LCD_MEM, SPRT_OR);
			}
			else DrawStr(70, 2 + (i-firstIndex)*10, buildings[buildingI[i-unitISize]].name, A_NORMAL);
		}
		
		if(firstIndex != 0)
		{
			DrawPix(155,1,A_NORMAL);
			DrawPix(154,2,A_NORMAL);
			DrawPix(155,2,A_NORMAL);
			DrawPix(156,2,A_NORMAL);
			DrawPix(153,3,A_NORMAL);
			DrawPix(154,3,A_NORMAL);
			DrawPix(155,3,A_NORMAL);
			DrawPix(156,3,A_NORMAL);
			DrawPix(157,3,A_NORMAL);
			DrawPix(155,4,A_NORMAL);
			DrawPix(155,5,A_NORMAL);
		}
		
		if(z != size)
		{
			DrawPix(155,98,A_NORMAL);
			DrawPix(154,97,A_NORMAL);
			DrawPix(155,97,A_NORMAL);
			DrawPix(156,97,A_NORMAL);
			DrawPix(153,96,A_NORMAL);
			DrawPix(154,96,A_NORMAL);
			DrawPix(155,96,A_NORMAL);
			DrawPix(156,96,A_NORMAL);
			DrawPix(157,96,A_NORMAL);
			DrawPix(155,95,A_NORMAL);
			DrawPix(155,94,A_NORMAL);
		}
			
		char j = index - firstIndex;
		bool redraw = 0;
		
		while(!redraw && key != KEY_ESC && key != KEY_ENTER)
		{
			Clr(0, 0, 68, 99);
			FontSetSys(F_6x8);
			
			char _i_ = index;
			
			if(_i_ < unitISize)
			{
				if(_units[unitI[_i_]].area == 1) 			DrawStr(0, 0, "Ground Unit", A_NORMAL);
				else if(_units[unitI[_i_]].area == 2) DrawStr(0, 0, "Sea Unit", A_NORMAL);
				else 																	DrawStr(0, 0, "Air Unit", A_NORMAL);
				
				char string[12];
				sprintf(string, "Atk:  %d", _units[unitI[_i_]].atk);
				DrawStr(0, 16, string, A_NORMAL);
				sprintf(string, "Def:  %d", _units[unitI[_i_]].def);
				DrawStr(0, 24, string, A_NORMAL);
				sprintf(string, "Move: %d", _units[unitI[_i_]].moveMAX);
				DrawStr(0, 32, string, A_NORMAL);
				
				sprintf(string, "Cost:  %d", _units[unitI[_i_]].cost);
				DrawStr(0, 80, string, A_NORMAL);
				if(civilization[0].city[city].production == 0)
					sprintf(string, "Turns: %d", 999);
				else
					sprintf(string, "Turns: %d", (_units[unitI[_i_]].cost - civilization[0].city[city].productionCurr) / civilization[0].city[city].production + 1);
				DrawStr(0, 88, string, A_NORMAL);
			}
			else
			{
				if(buildings[buildingI[_i_ - unitISize]].isWonder) 	DrawStr(0, 0, "Wonder", A_NORMAL);
				else																								DrawStr(0, 0, "Building", A_NORMAL);
				
				DrawStr(0, 16, buildngsDesc[buildingI[_i_ - unitISize]][0], A_NORMAL);
				DrawStr(0, 24, buildngsDesc[buildingI[_i_ - unitISize]][1], A_NORMAL);
				DrawStr(0, 32, buildngsDesc[buildingI[_i_ - unitISize]][2], A_NORMAL);
				DrawStr(0, 40, buildngsDesc[buildingI[_i_ - unitISize]][3], A_NORMAL);
				
				char string[12];
				sprintf(string, "Cost:  %d", buildings[buildingI[_i_ - unitISize]].cost);
				DrawStr(0, 80, string, A_NORMAL);
				if(civilization[0].city[city].production == 0)
					sprintf(string, "Turns: %d", 999);
				else
					sprintf(string, "Turns: %d", (buildings[buildingI[_i_ - unitISize]].cost - civilization[0].city[city].productionCurr) / civilization[0].city[city].production + 1);
				DrawStr(0, 88, string, A_NORMAL);
			}

			FontSetSys(F_4x6);
				
			Highlight2(70, j*10 - 1, 90);
			key = ngetchx();
			Highlight2(70, j*10 - 1, 90);
			
			if(key == KEY_DOWN)
			{
				if(index < size - 1)
				{
					index++;
					if(index > firstIndex + 9)
					{
						redraw = 1;
						if(firstIndex + 15 > size - 1)
							firstIndex = size - 10;
						else
							firstIndex += 5;
					} 
				}
			}
			else if(key == KEY_UP)
			{
				if(index > 0)
				{
					index--;
					if(index < firstIndex)
					{
						redraw = 1;
						if(firstIndex - 5 < 0)
							firstIndex = 0;
						else
							firstIndex -= 5;
					}
				}
			}
			
			j = index - firstIndex;
		}
	}
	
	if(key == KEY_ESC) return -1;
	return index;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Select /////////////////////////////////////////////////////////////////////////////////////////////////////////
// selects the items that are on the current grid square //////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Select() 
{
  //return variable
  bool update = 0;
  
	//scan for city
	char cityIndex = GetCityIndex(0);
	
	//scan for units
	char unitnumb[UNIT_NUMBER];
	GetUnitIndicesCursor(unitnumb);
	
	//menu strings
	char menuString[20][17];
	
	//write unit's and city's names to screen
	char word = 0, loop = 0;
	
	//write strings
	if (cityIndex != -1)         strcpy(menuString[word++], civilization[0].city[cityIndex].name);
	while (unitnumb[loop] != -1)
	{
		// char 15 == check
		// char 16 == box
		if(civilization[0].unit[unitnumb[loop]].moveCURR > 0) menuString[word][0] = 16;
		else                                                  menuString[word][0] = 15;
		
		strcpy(&menuString[word++][1], _units[civilization[0].unit[unitnumb[loop++]].type].name);
	}
	
	if(word == 0) return update;
	
	char i = SideMenu(menuString, word, 111, 32);
	ClrSide();
	
	if(i != -1)
	{
		if (cityIndex != -1)
		{
			//city stats
			if (i == 0)
			{
				DrawStr(111,33,civilization[0].city[cityIndex].name,A_NORMAL);
				update = ActionCity(cityIndex);
			}
			//unit stats if on a city
			else
			{
			  WriteUnit(unitnumb[i - 1]);
				ActionUnit(unitnumb[i - 1]);
			}
		}
		//unit stats if not on a city
		else
		{
		  WriteUnit(unitnumb[i]);
			ActionUnit(unitnumb[i]);
		}
	}
	
	ClrSide();
	GKeyFlush();	
	
	return update;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GetName ////////////////////////////////////////////////////////////////////////////////////////////////////////
// gets the name of the city //////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool GetName(char *name)
{
	HANDLE dlg = H_NULL;
	bool isValid = 1;
	
	if((dlg = DialogNewSimple(140,35)) != H_NULL)
	{
   	// format the dialog box
    DialogAddTitle(dlg,"New City",BT_OK,BT_NONE);
    DialogAddRequest(dlg,5,15,"Name:",0,15,16);
    
    int key = 0;
    while (key != KEY_ENTER && key != KEY_ESC) { key = DialogDo(dlg,CENTER,CENTER,name,NULL); }
    if(key == KEY_ESC) isValid = 0;
    
		FontSetSys(F_4x6);
	} 
	else isValid = 0;
	
	return isValid;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GetNumOfCivs ///////////////////////////////////////////////////////////////////////////////////////////////////
// gets the number of civilizations playing ///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char GetNumOfCivs()
{
	HANDLE dlg = H_NULL, menu = H_NULL;
	char num = 2;
	short int option = 0;
	
	if((dlg = DialogNewSimple(90,35)) != H_NULL)
	{
		if((menu = PopupNew(NULL,0)) != H_NULL)
		{
	    PopupAddText(menu,-1,"2",0);
	    PopupAddText(menu,-1,"3",1);
	    PopupAddText(menu,-1,"4",2);
	    
	    DialogAddTitle(dlg,"Players",BT_OK,BT_NONE);
    	DialogAddPulldown(dlg,5,15,"Number:",menu,0);
    
	    int key = 0;
	    while (key != KEY_ENTER && key != KEY_ESC) { key = DialogDo(dlg,CENTER,CENTER,NULL,&option); }
	    if(key == KEY_ENTER) num = option + 2;
    	
			FontSetSys(F_4x6);
		}
	} 
	
	return num;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GetMaxTurns ////////////////////////////////////////////////////////////////////////////////////////////////////
// determines the length of the game //////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int GetMaxTurns()
{
	HANDLE dlg = H_NULL, menu = H_NULL;
	short int option = 500;
	
	if((dlg = DialogNewSimple(120,35)) != H_NULL)
	{
		if((menu = PopupNew(NULL,0)) != H_NULL)
		{
	    PopupAddText(menu,-1,"Quick (300 turns)",300);
	    PopupAddText(menu,-1,"Normal (500 turns)",500);
	    PopupAddText(menu,-1,"Epic (800 turns)",800);
	    
	    DialogAddTitle(dlg,"Game Length",BT_OK,BT_NONE);
    	DialogAddPulldown(dlg,5,15,"Length:",menu,0);
    
	    int key = 0;
	    while (key != KEY_ENTER) { key = DialogDo(dlg,CENTER,CENTER,NULL,&option); }
    	
			FontSetSys(F_4x6);
		}
	} 
	
	return option;
}

char mapName[] = "civmap0";

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GetMapToLoad ///////////////////////////////////////////////////////////////////////////////////////////////////
// determines the map index to load ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char GetMapToLoad()
{
	HANDLE dlg = H_NULL, menu = H_NULL;
	short int option = -1;
	char i, num = 0;
	char name[20];
	
	if((dlg = DialogNewSimple(90,35)) == H_NULL) Crash(DIALOG, "");
	if((menu = PopupNew(NULL,0)) == H_NULL) Crash(DIALOG, "");
	
	for(i = 0; i < 10; i++)
	{
		mapName[6] = '0' + i;
		FILE *f = fopen(mapName,"rb");
		
		if(f != NULL)
		{
			game.mapSaveNum = i;
			fread(name, 20*sizeof(char),1,f);
			PopupAddText(menu,-1,name,i);
			if(option == -1) option = i;
			num++;
			fclose(f);
		}
	}
	
	// No available maps
	if(option == -1) Crash(MISSING_FILE, "No maps");
	if(num == 1) return option;
    
  DialogAddTitle(dlg,"Load Map",BT_OK,BT_NONE);
  DialogAddPulldown(dlg,5,15,"Map:",menu,option);
  
  int key = 0;
  while (key != KEY_ENTER && key != KEY_ESC) { key = DialogDo(dlg,CENTER,CENTER,NULL,&option); }
  
  if(key == KEY_ENTER) return option;
  return -1;
}

void _LoadMap(char index, bool freeMem)
{
  mapName[6] = '0' + index;
  	
  FILE *f = fopen(mapName,"rb");
 	if(f == NULL) Crash(DIALOG, "Opening Map File");
  	
  char w, h;
  char name[20];
  fread(name, 20*sizeof(char),1,f);
	fread(&w,sizeof(char),1,f);
	fread(&h,sizeof(char),1,f);
	game.mapWidth = w;
	game.mapHeight = h;
	if(freeMem)
	{
		Delete(game.worldmap);
		Delete(game.improv);
		Delete(game.bonusTerrain);
	}
	game.worldmap = (char*)Malloc(game.mapWidth * game.mapHeight * sizeof(char));
	game.improv = (bool*)Malloc(game.mapWidth * game.mapHeight * 4 * sizeof(bool));
	game.bonusTerrain = (char*)Malloc(game.mapWidth * game.mapHeight * sizeof(char));
	if(game.worldmap == NULL || game.improv == NULL || game.bonusTerrain == NULL)
	{
		Crash(MEMORY, "Map data");
	}
	
	fread(game.worldmap,game.mapWidth*game.mapHeight*sizeof(char),1,f);
  game.qXMax = (game.mapWidth - 11) / 5;
  game.qYMax = (game.mapHeight - 7) / 3;
  
  int i;
	for(i = 0; i < game.mapWidth * game.mapHeight * 4; i++)
		game.improv[i] = 0;
		
	fclose(f);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LoadMap ////////////////////////////////////////////////////////////////////////////////////////////////////////
// loads the map //////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void LoadMap()
{
	char index = GetMapToLoad();
  if(index != -1) _LoadMap(index, 0);
  else ExitCiv();
    	
	FontSetSys(F_4x6);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GetCivType /////////////////////////////////////////////////////////////////////////////////////////////////////
// gets the country of the main player ////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char GetCivType()
{
	HANDLE dlg = H_NULL, menu = H_NULL;
	char num = 0, i;
	short int option = 0;
	
	if((dlg = DialogNewSimple(90,35)) != H_NULL)
	{
		if((menu = PopupNew(NULL,0)) != H_NULL)
		{
			for(i = 0; i < CIV_TYPES; i++)
	    	PopupAddText(menu,-1,&game.civ_names[i*14],i);
	    
	    DialogAddTitle(dlg,"Civilization",BT_OK,BT_NONE);
    	DialogAddPulldown(dlg,5,15,"Country:",menu,0);
    
	    int key = 0;
	    while (key != KEY_ENTER && key != KEY_ESC) { key = DialogDo(dlg,CENTER,CENTER,NULL,&option); }
	    if(key == KEY_ENTER) num = option;
    	
			FontSetSys(F_4x6);
		}
	} 
	
	return num;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Confirm ////////////////////////////////////////////////////////////////////////////////////////////////////////
// asks yes or no /////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Confirm()
{
	HANDLE dlg = H_NULL;
	bool isValid = 0;
	
	if((dlg = DialogNewSimple(140,35)) != H_NULL)
	{
   	// format the dialog box
    DialogAddTitle(dlg,"Confirm",BT_OK,BT_CANCEL);
    DialogAddText(dlg,45,15,"Are you sure?");
    
    int key = 0;
    while (key != KEY_ENTER && key != KEY_ESC) { key = DialogDo(dlg,CENTER,CENTER,NULL,NULL); }
    if(key == KEY_ENTER) isValid = 1;
    
		FontSetSys(F_4x6);
	} 
	else isValid = 0;
	
	return isValid;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TopMenu ////////////////////////////////////////////////////////////////////////////////////////////////////////
// draws the text in the top menu /////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TopMenu()
{
	FontSetSys(F_8x10);
	DrawStr(0,0, &game.civ_names[civilization[0].type*14], A_NORMAL);
	
	FontSetSys(F_4x6);
	char gold[20], population[25], turnStr[15];
	
	sprintf(gold, "Gold: %d", civilization[0].gold);
	sprintf(population, "%d0000 people", CalculatePopulation());
	sprintf(turnStr, "Turn: %d", game.turn);
	
	DrawStr(65, 3, gold, A_NORMAL);
	DrawStr(3, 12, population, A_NORMAL);
	DrawStr(65, 12, turnStr, A_NORMAL);
	
	if(civilization[0].currentTechResearch != -1)
	{
		DrawLine(10, 19, 100, 19, A_NORMAL);
		DrawLine(10, 27, 100, 27, A_NORMAL);
		DrawLine(10, 19,  10, 27, A_NORMAL);
		DrawLine(100,19, 100, 27, A_NORMAL);
		
		DrawStr(20, 21, techs[civilization[0].currentTechResearch].name, A_NORMAL);
		
		float percent = civilization[0].researchAmount / (float)((techs[civilization[0].currentTechResearch].epoch+1)*50);
		int dist = percent * 88, i;
		for(i = 20; i < 27; i++)
			DrawLine(11, i, 11+dist, i, A_XOR);
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// UpdateCursorMenu ///////////////////////////////////////////////////////////////////////////////////////////////
// draws the text in the top, right corner menu ///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void UpdateCursorMenu()
{
	ClrCursorMenu();
	
	if(!GetBit(civilization[0].fog, (GetCursorY()-1)*game.mapWidth+(GetCursorX()-1)))
	{
		DrawStr(112, 6, "Unknown", A_NORMAL);
		return;
	}
	
	DrawStr(112, 6, terrain[Worldmap(GetCursorY()-1,GetCursorX()-1)-1].name, A_NORMAL);
	
	char i = Bonus(GetCursorY()-1,GetCursorX()-1);
	if(i != -1) DrawStr(112, 13, tBonus[i].name, A_NORMAL);
	
	if(Improv(GetCursorY()-1,GetCursorX()-1,0)) // road
	{
		DrawLine(117, 20, 118, 26, A_NORMAL);
		DrawLine(118, 20, 119, 26, A_NORMAL);
	}
	if(Improv(GetCursorY()-1,GetCursorX()-1,1)) // irrigation
	{
		DrawLine(127, 20, 129, 26, A_NORMAL);
		DrawLine(131, 20, 133, 26, A_NORMAL);
		DrawLine(126, 21, 134, 22, A_NORMAL);
		DrawLine(127, 24, 134, 25, A_NORMAL);
	}
	if(Improv(GetCursorY()-1,GetCursorX()-1,2)) // mine
	{
		DrawLine(140, 23, 140, 26, A_NORMAL);
		DrawLine(141, 22, 141, 26, A_NORMAL);
		DrawLine(142, 22, 142, 26, A_NORMAL);
		DrawLine(143, 23, 143, 26, A_NORMAL);
	}
	if(Improv(GetCursorY()-1,GetCursorX()-1,3)) // railroad
	{
		DrawLine(150, 20, 150, 26, A_NORMAL);
		DrawLine(152, 20, 152, 26, A_NORMAL);
		DrawPix(151, 21, A_NORMAL);
		DrawPix(151, 23, A_NORMAL);
		DrawPix(151, 25, A_NORMAL);
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CursorMenu /////////////////////////////////////////////////////////////////////////////////////////////////////
// draws the text in the top, right corner menu ///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CursorMenu()
{
	DrawStr(116,0, "Cursor", A_NORMAL);
	DrawLine(115, 2, 111,  2, A_NORMAL);
	DrawLine(140, 2, 159,  2, A_NORMAL);
	DrawLine(111, 2, 111, 28, A_NORMAL);
	DrawLine(111,28, 159, 28, A_NORMAL);
	DrawLine(159, 2, 159, 28, A_NORMAL);
	
	UpdateCursorMenu();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DisplayCursorStats /////////////////////////////////////////////////////////////////////////////////////////////
// displays the stats of the land at the cursor ///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool DisplayCursorStats()
{
	int x = GetCursorX()-1;
	int y = GetCursorY()-1;
	
	if(!GetBit(civilization[0].fog, x + y * game.mapWidth)) return 0;
	
	DrawLine(30,20,130,20, A_NORMAL);
	DrawLine(30,75,130,75, A_NORMAL);
	DrawLine(30,20,30,75, A_NORMAL);
	DrawLine(130,20,130,75, A_NORMAL);
	
	Clr(31, 21, 129, 74);
		
	int index = Worldmap(y,x)-1;
	int bonus = Bonus(y,x);
		
	FontSetSys(F_6x8);
	DrawStr(32, 22, terrain[index].name, A_NORMAL);
	FontSetSys(F_4x6);
	
	// strings
	char move[20], defense[15], food[15], shield[15], trade[15];
	
	char _food = terrain[index].food;
	char _shield = terrain[index].shields;
	char _trade = terrain[index].trade;
	
	if(bonus != -1)
	{
		_food += tBonus[bonus].food;
		_shield += tBonus[bonus].shields;
		_trade += tBonus[bonus].trade;
	}
	
	if(HasRoad(x,y) && terrain[index].irrigate) 				_trade += 1;
  if(HasIrrigation(x,y)) 															_food += terrain[index].iBonus;
  if(HasMine(x,y)) 																		_shield += terrain[index].mBonus;
  if(HasRailroad(x,y) && terrain[index].irrigate) 		_trade += 1;
	
	// create string
	sprintf(move, "Movement Cost: %d", terrain[index].movecost);
	sprintf(defense, "Defense: %d%%", terrain[index].defense * 50);
	sprintf(food, "Food: %d", _food);
	sprintf(shield, "Shield: %d", _shield);
	sprintf(trade, "Trade: %d", _trade);
	
	// draw strings
	DrawStr(34, 32, move, A_NORMAL);
	DrawStr(34, 40, defense, A_NORMAL);
	DrawStr(34, 49, food, A_NORMAL);
	DrawStr(34, 58, shield, A_NORMAL);
	DrawStr(34, 67, trade, A_NORMAL);
	
	if(HasRoad(x,y)) 				DrawStr(112, 40, "Road", A_NORMAL);
	if(HasIrrigation(x,y)) 	DrawStr( 97, 49, "Irrigated", A_NORMAL);
	if(HasMine(x,y)) 				DrawStr(112, 58, "Mine", A_NORMAL);
	if(HasRailroad(x,y)) 		DrawStr( 99, 67, "Railroad", A_NORMAL);
	
	ngetchx();
	
	return 1;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ConfirmOverwrite ///////////////////////////////////////////////////////////////////////////////////////////////
// confirms the overwrite of a map file ///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ConfirmOverwrite()
{
	HANDLE dlg = H_NULL;
	bool isValid = 0;
	
	if((dlg = DialogNewSimple(140,35)) != H_NULL)
	{
   	// format the dialog box
    DialogAddTitle(dlg,"Confirm",BT_OK,BT_CANCEL);
    DialogAddText(dlg,25,15,"Overwrite current file?");
    
    int key = 0;
    while (key != KEY_ENTER && key != KEY_ESC) { key = DialogDo(dlg,CENTER,CENTER,NULL,NULL); }
    if(key == KEY_ENTER) isValid = 1;
    
		FontSetSys(F_4x6);
	} 
	
	return isValid;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GetScenarioName ////////////////////////////////////////////////////////////////////////////////////////////////
// gets a name for the scenario save file /////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void GetScenarioName(char *name)
{
	HANDLE dlg = H_NULL;
	
	if((dlg = DialogNewSimple(140,35)) == H_NULL) Crash(DIALOG, "");
		
  // format the dialog box
  DialogAddTitle(dlg,"Save Map",BT_OK,BT_NONE);
  DialogAddRequest(dlg,5,15,"Name:",0,15,16);
    
  int key = 0;
  while (key != KEY_ENTER) { key = DialogDo(dlg,CENTER,CENTER,name,NULL); }
    
	FontSetSys(F_4x6);
}

char civsav[] = "civsave0";

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GetFile ////////////////////////////////////////////////////////////////////////////////////////////////////////
// gets the file to save or load //////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char GetFile(char name[20])
{
	int key = 0;
	char index = 0;
	char number[2] = "0";
	bool empty = 0;
	
	while(key != KEY_ESC && key != KEY_ENTER)
	{
		ClrScr();
		FontSetSys(F_8x10);
		char i;
		for(i = 0; i < 10; i++)
		{
			DrawLine(5 + 15*i,  4, 15 + 15*i,  4, A_NORMAL);
			DrawLine(5 + 15*i, 16, 15 + 15*i, 16, A_NORMAL);
			DrawLine(5 + 15*i,  4,  5 + 15*i, 16, A_NORMAL);
			DrawLine(15 + 15*i, 4, 15 + 15*i, 16, A_NORMAL);
			
			number[0] = i + '0';
			DrawStr(6 + 15*i, 6, number, A_NORMAL);
		}
		
		DrawLine(4 + 15*index,  3, 16 + 15*index,  3, A_NORMAL);
		DrawLine(4 + 15*index, 17, 16 + 15*index, 17, A_NORMAL);
		DrawLine(4 + 15*index,  3,  4 + 15*index, 17, A_NORMAL);
		DrawLine(16+ 15*index,  3, 16 + 15*index, 17, A_NORMAL);

		// open file for writing
		civsav[7] = '0' + index;
		FILE *f = fopen(civsav,"rb");
		
		if(f == NULL)
		{
			FontSetSys(F_6x8);
			DrawStr(5, 28, "Empty", A_NORMAL);
			FontSetSys(F_4x6);
			empty = 1;
			strcpy(name, "");
		}
		else 
		{
			// write structure data to the file
			fread(name, 20*sizeof(char),1,f);
	
			// close the file
			fclose(f);
			
			//sprintf(width, "Width: %d", w);
			//sprintf(height, "Height: %d", h);
			
			FontSetSys(F_6x8);
			DrawStr(5, 28, name, A_NORMAL);
			
			FontSetSys(F_4x6);
			empty = 0;
		}
		
		key = ngetchx();
		
		if(key == KEY_LEFT && --index < 0) index = 9;
		else if(key == KEY_RIGHT && ++index > 9) index = 0;
	}
	
	if(key == KEY_ENTER) return index + (empty * 100);
	return -1;
}

bool Save(char file, char *name)
{
	ClrScr();
	char line = 1;
	DrawStr(0,0, "Saving...", A_NORMAL);
	
	// set file
	civsav[7] = '0' + file;
	
	// open file for writing
	FILE *f;
	if ((f = fopen(civsav,"wb")) == NULL) Crash(DIALOG, "Saving file.");
		
	// write structure data to the file
	DrawStr(5, 2+7*line++, "Map Data...", A_NORMAL);
	fwrite(name, 20*sizeof(char),1,f);
	fwrite(&game.mapSaveNum, sizeof(char),1,f);
	fwrite(game.improv, game.mapWidth*game.mapHeight*4*sizeof(char),1,f);
	fwrite(&game.randomSeed, sizeof(long),1,f);
	fwrite(&game.turn, sizeof(int),1,f);
	fwrite(&game.maxTurns, sizeof(int),1,f);
	fwrite(&civNumb, sizeof(char),1,f);
	
	DrawStr(5, 2+7*line++, "Civ Data...", A_NORMAL);
	// for each civ
	char i;
	for(i = 0; i < civNumb; i++)
	{
		char str[10];
		sprintf(str, "Civ - %d...", i+1);
		DrawStr(10, 2+7*line++, str, A_NORMAL);
		
		fwrite(&civilization[i].type, sizeof(char),1,f);
		fwrite(&civilization[i].currentName, sizeof(char),1,f);
		fwrite(&civilization[i].gold, sizeof(int),1,f);
		fwrite(&civilization[i].luxuryPercent, sizeof(char),1,f);
		fwrite(&civilization[i].sciencePercent, sizeof(char),1,f);
		fwrite(civilization[i].techs, (TECH_NUMBER/8+1)*sizeof(char),1,f);
		fwrite(&civilization[i].currentTechResearch, sizeof(char),1,f);
		fwrite(&civilization[i].researchAmount, sizeof(int),1,f);
		fwrite(civilization[i].fog, ((game.mapWidth*game.mapHeight)/8+1) * sizeof(char),1,f);
		
		fwrite(civilization[i].inter.isKnown, civNumb*sizeof(char),1,f);
		fwrite(civilization[i].inter.relation, civNumb*sizeof(char),1,f);
		
		fwrite(&civilization[i].unitSize, sizeof(char),1,f);
		
		char j;
		for(j = 0; j < civilization[i].unitSize; j++)
		{
			fwrite(&civilization[i].unit[j].x, sizeof(int),1,f);
			fwrite(&civilization[i].unit[j].y, sizeof(int),1,f);
			fwrite(&civilization[i].unit[j].type, sizeof(char),1,f);
			fwrite(&civilization[i].unit[j].hpFULL, sizeof(char),1,f);
			fwrite(&civilization[i].unit[j].hpCURR, sizeof(char),1,f);
			fwrite(&civilization[i].unit[j].moveCURR, sizeof(char),1,f);
			fwrite(&civilization[i].unit[j].settlerAction, sizeof(char),1,f);
			fwrite(&civilization[i].unit[j].settlerActionTime, sizeof(char),1,f);
			fwrite(&civilization[i].unit[j].isFortified, sizeof(char),1,f);
			fwrite(&civilization[i].unit[j].xTarget, sizeof(int),1,f);
			fwrite(&civilization[i].unit[j].yTarget, sizeof(int),1,f);
		}
		
		fwrite(&civilization[i].citySize, sizeof(char),1,f);
		
		for(j = 0; j < civilization[i].citySize; j++)
		{
			fwrite(&civilization[i].city[j].x, sizeof(int),1,f);
			fwrite(&civilization[i].city[j].y, sizeof(int),1,f);
			fwrite(&civilization[i].city[j].name, 16*sizeof(char),1,f);
			fwrite(&civilization[i].city[j].population, sizeof(char),1,f);
			fwrite(&civilization[i].city[j].foodStorage, sizeof(char),1,f);
			fwrite(&civilization[i].city[j].resourceEmph, sizeof(char),1,f);
			fwrite(&civilization[i].city[j].underProduction, sizeof(char),1,f);
			fwrite(&civilization[i].city[j].underProdType, sizeof(char),1,f);
			fwrite(&civilization[i].city[j].productionCurr, sizeof(int),1,f);
			fwrite(civilization[i].city[j].buildings, (BUILDING_NUMBER/8+1)*sizeof(char),1,f);
		}
	}
	
	DrawStr(5, 2+7*line++, "AI Data...", A_NORMAL);
	// for each ai
	for(i = 0; i < civNumb-1; i++)
	{
		fwrite(&aiCivs[i].type, sizeof(char),1,f);
		fwrite(&aiCivs[i].autoSettlers, sizeof(char),1,f);
		fwrite(aiCivs[i].attitude, civNumb*sizeof(char),1,f);
	}

	// append the file ID tag
	fputc(0,f);
	fputs("civ",f);
	fputc(0,f);
	fputc(OTH_TAG,f);
	
	// close the file
	fclose(f);
	
	line++;
	DrawStr(0, 2+7*line, "File Saved.", A_NORMAL);
	
	return 1;
}

bool Load(char file, bool freeMem)
{
	ClrScr();
	char line = 1;
	DrawStr(0,0, "Loading...", A_NORMAL);

	// set file
	civsav[7] = '0' + file;
	
	// open file for writing
	FILE *f;
	if ((f = fopen(civsav,"rb")) == NULL) Crash(DIALOG, "Loading file.");
		
	// write structure data to the file
	DrawStr(5, 2+7*line++, "Map Data...", A_NORMAL);
	char name[20];
	fread(name, 20*sizeof(char),1,f);
	fread(&game.mapSaveNum, sizeof(char),1,f);
	fclose(f);
	
	_LoadMap(game.mapSaveNum, freeMem);
	if ((f = fopen(civsav,"rb")) == NULL) Crash(DIALOG, "Loading file.");
	fread(name, 20*sizeof(char),1,f);
	fread(&game.mapSaveNum, sizeof(char),1,f);
	
	fread(game.improv, game.mapWidth*game.mapHeight*4*sizeof(char),1,f);
	fread(&game.randomSeed, sizeof(long),1,f);
	fread(&game.turn, sizeof(int),1,f);
	fread(&game.maxTurns, sizeof(int),1,f);
	
	// Bonus Terrain
	GenerateBonusTerrain(game.randomSeed);
	
	DrawStr(5, 2+7*line++, "Civ Data...", A_NORMAL);
	// clear old memory
	int i;
	if(freeMem) FreeCivData();
	
	fread(&civNumb, sizeof(char),1,f);

	if(AllocateCivData(civNumb) == 0)
	{
		fclose(f);
		Crash(MEMORY, "Allocating Civ Data");
	}
	
	// for each civ
	for(i = 0; i < civNumb; i++)
	{
		char str[10];
		sprintf(str, "Civ - %d...", i+1);
		DrawStr(10, 2+7*line++, str, A_NORMAL);
		
		fread(&civilization[i].type, sizeof(char),1,f);
		fread(&civilization[i].currentName, sizeof(char),1,f);
		fread(&civilization[i].gold, sizeof(int),1,f);
		fread(&civilization[i].luxuryPercent, sizeof(char),1,f);
		fread(&civilization[i].sciencePercent, sizeof(char),1,f);
		fread(civilization[i].techs, (TECH_NUMBER/8+1)*sizeof(char),1,f);
		fread(&civilization[i].currentTechResearch, sizeof(char),1,f);
		fread(&civilization[i].researchAmount, sizeof(int),1,f);
		fread(civilization[i].fog, ((game.mapWidth*game.mapHeight)/8+1) * sizeof(char),1,f);
		
		fread(civilization[i].inter.isKnown, civNumb*sizeof(char),1,f);
		fread(civilization[i].inter.relation, civNumb*sizeof(char),1,f);
		
		fread(&civilization[i].unitSize, sizeof(char),1,f);
		
		char j;
		for(j = 0; j < civilization[i].unitSize; j++)
		{
			fread(&civilization[i].unit[j].x, sizeof(int),1,f);
			fread(&civilization[i].unit[j].y, sizeof(int),1,f);
			fread(&civilization[i].unit[j].type, sizeof(char),1,f);
			fread(&civilization[i].unit[j].hpFULL, sizeof(char),1,f);
			fread(&civilization[i].unit[j].hpCURR, sizeof(char),1,f);
			fread(&civilization[i].unit[j].moveCURR, sizeof(char),1,f);
			fread(&civilization[i].unit[j].settlerAction, sizeof(char),1,f);
			fread(&civilization[i].unit[j].settlerActionTime, sizeof(char),1,f);
			fread(&civilization[i].unit[j].isFortified, sizeof(char),1,f);
			fread(&civilization[i].unit[j].xTarget, sizeof(int),1,f);
			fread(&civilization[i].unit[j].yTarget, sizeof(int),1,f);
		}
		
		fread(&civilization[i].citySize, sizeof(char),1,f);
		
		for(j = 0; j < civilization[i].citySize; j++)
		{
			fread(&civilization[i].city[j].x, sizeof(int),1,f);
			fread(&civilization[i].city[j].y, sizeof(int),1,f);
			fread(&civilization[i].city[j].name, 16*sizeof(char),1,f);
			fread(&civilization[i].city[j].population, sizeof(char),1,f);
			fread(&civilization[i].city[j].foodStorage, sizeof(char),1,f);
			fread(&civilization[i].city[j].resourceEmph, sizeof(char),1,f);
			fread(&civilization[i].city[j].underProduction, sizeof(char),1,f);
			fread(&civilization[i].city[j].underProdType, sizeof(char),1,f);
			fread(&civilization[i].city[j].productionCurr, sizeof(int),1,f);
			fread(civilization[i].city[j].buildings, (BUILDING_NUMBER/8+1)*sizeof(char),1,f);
			
			CalculateResources(i, j);
			CalculateHappiness(i, j);
		}
	}
	
	DrawStr(5, 2+7*line++, "AI Data...", A_NORMAL);
	// for each ai
	for(i = 0; i < civNumb-1; i++)
	{
		fread(&aiCivs[i].type, sizeof(char),1,f);
		fread(&aiCivs[i].autoSettlers, sizeof(char),1,f);
		fread(aiCivs[i].attitude, civNumb*sizeof(char),1,f);
	}
	
	if(civilization[0].citySize > 0)
		SetCursor(civilization[0].city[0].x, civilization[0].city[0].y);
	else
		SetCursor(civilization[0].unit[0].x, civilization[0].unit[0].y);
	
	// close the file
	fclose(f);
	
	line++;
	DrawStr(0, 2+7*line, "File Loaded.", A_NORMAL);
	
	return 1;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SaveScenario ///////////////////////////////////////////////////////////////////////////////////////////////////
// saves all the current data for the scenario ////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool SaveScenario()
{
	char name[20];
	char index = GetFile(name);
	if(index != -1)
	{
		if(index < 100) { if(!ConfirmOverwrite()) return 0; }
		else	index -= 100;
		
		// Get a name for scenario
		GetScenarioName(name);
		
		// Save the file
		return Save(index, name);
	}
	return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LoadScenario ///////////////////////////////////////////////////////////////////////////////////////////////////
// loads all the current data for the scenario ////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool LoadScenario(bool freeMem)
{
	char name[20];
	char index = GetFile(name);
	if(index != -1 && index < 100)
	{
		// Save the file
		return Load(index, freeMem);
	}
	return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ChangeProduction ///////////////////////////////////////////////////////////////////////////////////////////////
// confirms the changing of production after something is built ///////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ChangeProduction(char *city, char *object)
{
	HANDLE dlg = H_NULL;
	bool isValid = 0;
	
	if((dlg = DialogNewSimple(140,45)) != H_NULL)
	{
   	// format the dialog box
    DialogAddTitle(dlg,"Production",BT_OK,BT_CANCEL);
    
    char text[30], text2[45];
    sprintf(text, "%s just finished a", city);
    sprintf(text2, "%s. Change Production?", object);
    
    DialogAddText(dlg,10,15,text);
    DialogAddText(dlg,10,25,text2);
    
    int key = 0;
    while (key != KEY_ENTER && key != KEY_ESC) { key = DialogDo(dlg,CENTER,CENTER,NULL,NULL); }
    if(key == KEY_ENTER) isValid = 1;
    
		FontSetSys(F_4x6);
	} 
	
	return isValid;
}
bool ChangeProduction2(char *city)
{
	HANDLE dlg = H_NULL;
	bool isValid = 0;
	
	if((dlg = DialogNewSimple(140,45)) != H_NULL)
	{
   	// format the dialog box
    DialogAddTitle(dlg,"Production",BT_OK,BT_CANCEL);
    
    char text[40];
    sprintf(text, "%s isn\'t producting anything.", city);
    
    DialogAddText(dlg,5,15,text);
    DialogAddText(dlg,30,25,"Change Production?");
    
    int key = 0;
    while (key != KEY_ENTER && key != KEY_ESC) { key = DialogDo(dlg,CENTER,CENTER,NULL,NULL); }
    if(key == KEY_ENTER) isValid = 1;
    
		FontSetSys(F_4x6);
	} 
	
	return isValid;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Preview ////////////////////////////////////////////////////////////////////////////////////////////////////////
// it searches the cursor square for units & cities ///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Preview()
{
	//clear the side
	ClrSide();
	
	//scan for city
	char civIndex = 0;
	char cityIndex = -1;
	while(civIndex < civNumb && cityIndex == -1)
		cityIndex = GetCityIndex(civIndex++);
	civIndex--;
	
	if(cityIndex != -1)
	{
		// city name
		DrawStr(111, 32, civilization[civIndex].city[cityIndex].name, A_NORMAL);
		
		// city population
		char string[25];
		sprintf(string, "Size: %d", civilization[civIndex].city[cityIndex].population);
		DrawStr(111, 41, string, A_NORMAL);
		
		if(civIndex == 0)
		{
			// production
			if(civilization[0].city[cityIndex].underProduction != -1)
			{
				char prod[30];
				if(civilization[0].city[cityIndex].underProdType == 1)
					sprintf(prod, "%d%% - %s", 100 * civilization[0].city[cityIndex].productionCurr / _units[civilization[0].city[cityIndex].underProduction].cost, _units[civilization[0].city[cityIndex].underProduction].name);
				else
					sprintf(prod, "%d%% - %s", 100 * civilization[0].city[cityIndex].productionCurr / buildings[civilization[0].city[cityIndex].underProduction].cost, buildings[civilization[0].city[cityIndex].underProduction].name);
					
				DrawStr(115, 50, prod, A_NORMAL);
			}
			else DrawStr(115,50,"Nothing", A_NORMAL);
		}
		else DrawStr(111,50,&game.civ_names[civilization[civIndex].type*14], A_NORMAL);
			
		//scan for units
		char num = GetNumberOfUnitsCursor(civIndex);
		if(num != 0)
		{
			char numUnits[15];
			sprintf(numUnits, "%d Unit(s)", num);
			DrawStr(111, 59, numUnits, A_NORMAL);
		}
	}
	else
	{
		//scan for units
		char num = 0;
		civIndex = 0;
		while(civIndex < civNumb && num == 0)
			num = GetNumberOfUnitsCursor(civIndex++);
		civIndex--;
		
		if(num != 0)
		{
			char numUnits[15];
			sprintf(numUnits, "%d Unit(s)", num);
			DrawStr(111, 32, numUnits, A_NORMAL);
			if(civIndex != 0)
				DrawStr(111, 41, &game.civ_names[civilization[civIndex].type*14], A_NORMAL);
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CheatMenu //////////////////////////////////////////////////////////////////////////////////////////////////////
// shows the cheat menu for the game //////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CheatMenu()
{
	DrawLine( 35, 24, 115, 24, A_NORMAL);
	DrawLine( 35, 76, 115, 76, A_NORMAL);
	DrawLine( 35, 24,  35, 76, A_NORMAL);
	DrawLine(115, 24, 115, 76, A_NORMAL);
	
	Clr(36, 25, 114, 75);
	
	FontSetSys(F_6x8);
	
	DrawStr(45, 27, "Reveal Map", A_NORMAL);
	DrawStr(48, 37, "All Techs", A_NORMAL);
	DrawStr(48, 47, "Give Gold", A_NORMAL);
	DrawStr(58, 57, "------", A_NORMAL);
	DrawStr(63, 67, "Back", A_NORMAL);
	
	FontSetSys(F_4x6);
	
	char i = 0;
	int key = 0;
	
	while(key != KEY_ENTER)
	{
		Highlight2(36, 25 + i*10, 78);
		key = ngetchx();
		Highlight2(36, 25 + i*10, 78);
		
		if(key == KEY_UP)
		{
			if(--i < 0) i = 4;
		}
		else if(key == KEY_DOWN)
		{
			if(++i > 4) i = 0;
		}
		else if(key == KEY_ESC)
		{
			i = 4;
		}
	}
	
	int j, size = game.mapWidth * game.mapHeight / 8 + 1;
	
	switch(i)
	{
		case 0:
			for(j = 0; j < size; j++)
				SetByteTrue(civilization[0].fog, j);
			for(j = 0; j < civNumb; j++)
			{
				civilization[0].inter.isKnown[j] = 1;
				civilization[j].inter.isKnown[0] = 1;
			}
			DrawScreen();
			break;
		case 1:
			for(j = 0; j < TECH_NUMBER/8+1; j++)
				civilization[0].techs[j] = 255;
			civilization[0].currentTechResearch = 89;
			civilization[0].researchAmount = 0;
			break;
		case 2:
			civilization[0].gold = 9999;
			DrawScreen();
			break;
		case 3:
		
			break;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MainMenu ///////////////////////////////////////////////////////////////////////////////////////////////////////
// shows the main menu for the game ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool MainMenu()
{
	char i = 0;
	int key = 0;
	bool draw = 1;
	
	while(1)
	{
		if(draw)
		{
			DrawLine( 35, 24, 115, 24, A_NORMAL);
			DrawLine( 35, 76, 115, 76, A_NORMAL);
			DrawLine( 35, 24,  35, 76, A_NORMAL);
			DrawLine(115, 24, 115, 76, A_NORMAL);
			
			Clr(36, 25, 114, 75);
			
			FontSetSys(F_6x8);
			
			DrawStr(51, 27, "Continue", A_NORMAL);
			DrawStr(48, 37, "Save Game", A_NORMAL);
			DrawStr(48, 47, "Load Game", A_NORMAL);
			DrawStr(58, 57, "Cheats", A_NORMAL);
			DrawStr(63, 67, "Exit", A_NORMAL);
			
			FontSetSys(F_4x6);
			draw = 0;
		}
	
	
		Highlight2(36, 25 + i*10, 78);
		key = ngetchx();
		Highlight2(36, 25 + i*10, 78);
		
		switch(key)
		{
			case KEY_UP:
				if(--i < 0) i = 4;
				break;
			case KEY_DOWN:
				if(++i > 4) i = 0;
				break;
			case KEY_ESC:
				i = 4;
				break;
			case KEY_ENTER:
				switch(i)
				{
					case 0: // Continue
						return 1;
						break;
					case 1: // Save Game
						SaveScenario();
						DrawScreen();
						draw = 1;
						break;
					case 2: // Load Game
						LoadScenario(1);
						DrawScreen();
						draw = 1;
						break;
					case 3: // Cheats
						CheatMenu();
						draw = 1;
						break;
					case 4: // Exit
						return 0;
						break;
				}
				break;
		}
	}
}

